package com.ruoyi.mf178.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mf178.domain.Mf178TradeResponse;
import com.ruoyi.mf178.service.IMf178CouponTradeService;
import com.ruoyi.mf178.vo.OrderStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import com.ruoyi.mf178.mapper.Mf178OrderMapper;
import com.ruoyi.mf178.domain.Mf178Order;
import com.ruoyi.mf178.service.IMf178OrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * mf178Service业务层处理
 *
 * @author ruoyi
 * @date 2025-07-14
 */
@Service
public class Mf178OrderServiceImpl implements IMf178OrderService {
    Logger log = LoggerFactory.getLogger(Mf178OrderServiceImpl.class);

    @Autowired
    private Mf178OrderMapper mf178OrderMapper;
    @Autowired
    private ApplicationContext applicationContext;


    /**
     * 查询mf178
     *
     * @param id mf178主键
     * @return mf178
     */
    @Override
    public Mf178Order selectMf178OrderById(Long id) {
        return mf178OrderMapper.selectMf178OrderById(id);
    }

    @Override
    public Mf178Order selectMf178OrderByExternalOrderNo(String externalOrderNo) {
        return mf178OrderMapper.selectMf178OrderByExternalOrderNo(externalOrderNo);
    }

    /**
     * 查询mf178列表
     *
     * @param mf178Order mf178
     * @return mf178
     */
    @Override
    public List<Mf178Order> selectMf178OrderList(Mf178Order mf178Order) {
        return mf178OrderMapper.selectMf178OrderList(mf178Order);
    }

    /**
     * 新增mf178
     *
     * @param mf178Order mf178
     * @return 结果
     */
    @Override
    public int insertMf178Order(Mf178Order mf178Order) {
        mf178Order.setCreateTime(DateUtils.getNowDate());
        return mf178OrderMapper.insertMf178Order(mf178Order);
    }

    /**
     * 修改mf178
     *
     * @param mf178Order mf178
     * @return 结果
     */
    @Override
    public int updateMf178Order(Mf178Order mf178Order) {
        mf178Order.setUpdateTime(DateUtils.getNowDate());
        return mf178OrderMapper.updateMf178Order(mf178Order);
    }

    /**
     * 批量删除mf178
     *
     * @param ids 需要删除的mf178主键
     * @return 结果
     */
    @Override
    public int deleteMf178OrderByIds(Long[] ids) {
        return mf178OrderMapper.deleteMf178OrderByIds(ids);
    }

    /**
     * 删除mf178信息
     *
     * @param id mf178主键
     * @return 结果
     */
    @Override
    public int deleteMf178OrderById(Long id) {
        return mf178OrderMapper.deleteMf178OrderById(id);
    }

    @Override
    public AjaxResult dropCard(List<Mf178Order> mf178Order) {
        String errorString = "";
        for (Mf178Order order : mf178Order) {
            try {
                IMf178CouponTradeService service = applicationContext.getBean(order.getGoodSupplierService(), IMf178CouponTradeService.class);
                String result = service.dropCard(order);
                JSONObject resultJson = JSONObject.parse(result);
                Integer code = resultJson.getInteger("code");
                if (code != null && code.equals(200)) {
                    int i = mf178OrderMapper.updateMf178OrderCardDrop(order.getId(), true);
                    if (i == 0) {
                        errorString = errorString + order.getExternalOrderNo() + ",";
                    }
                } else {
                    errorString = errorString + order.getExternalOrderNo() + ",";
                }

            } catch (NoSuchBeanDefinitionException e) {
                log.error("调用dropCard实现类异常，", e);
                errorString = errorString + order.getExternalOrderNo() + ",";
            }
        }
        if (StringUtils.isBlank(errorString)) {
            return AjaxResult.success();
        }
        return AjaxResult.error("以下ExternalOrderNo作废失败：" + errorString);
    }

    @Override
    public AjaxResult syncCardStatus(List<Mf178Order> mf178Order) {
        String errorString = "";
        for (Mf178Order order : mf178Order) {
            try {
                IMf178CouponTradeService service = applicationContext.getBean(order.getGoodSupplierService(), IMf178CouponTradeService.class);
                String result = service.cardStatus(order);
                JSONObject resultJson = JSONObject.parse(result);
                Integer code = resultJson.getInteger("code");
                if (code != null && code.equals(200)) {
                    JSONObject data = resultJson.getJSONObject("data");
                    int i = mf178OrderMapper.updateMf178OrderCardStatus(order.getId(), data.getBoolean("cardUsed"),
                            data.getDate("usedTime"), data.getString("pickUpRemark"),
                            data.getBoolean("isCardDrop"), data.getBoolean("usedSucess"));
                    if (i == 0) {
                        errorString = errorString + order.getExternalOrderNo() + ",";
                    }
                } else {
                    errorString = errorString + order.getExternalOrderNo() + ",";
                }

            } catch (NoSuchBeanDefinitionException e) {
                log.error("调用cardStatus实现类异常，", e);
                errorString = errorString + order.getExternalOrderNo() + ",";
            }
        }
        if (StringUtils.isBlank(errorString)) {
            return AjaxResult.success();
        }
        return AjaxResult.error("以下ExternalOrderNo同步失败：" + errorString);
    }

    @Override
    public void setLog(String externalOrderNo, String request, String response) {
        mf178OrderMapper.setLog(externalOrderNo, request, response);
    }
}
